package com.org.zp.ctrl.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.HttpUtils.CallBack;
import com.org.base.widget.LoadMoreListView;
import com.org.base.widget.LoadMoreListView.OnLoadMoreListener;
import com.org.base.widget.LoadMoreListViewWithProgress;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.entity.job.Artical;

public class BusinessSchoolListActivity extends BaseActivity {
	private PaginationAdapter adapter;
	private TextView title;
	private Button back_btn;
	private Button enable_btn;
	
	private String id = "";
	private ArrayList<Artical> artical = new ArrayList<Artical>();
	
	private LoadMoreListViewWithProgress viewWhitProgress;
	private LoadMoreListView listView;
	private LinearLayout linear;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.job_class);
		
		if (!(ActivityTaskManager.getInstance().containsName("BusinessSchoolListActivity"))) {
			ActivityTaskManager.getInstance().putActivity("BusinessSchoolListActivity", this);
		}
		
		title = (TextView) findViewById(R.id.headTitle);
		title.setText(getIntent().getStringExtra("title"));
		back_btn = (Button) findViewById(R.id.back);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity("BusinessSchoolListActivity");
			}
		});
		enable_btn = (Button) findViewById(R.id.top_right_button);
		enable_btn.setVisibility(View.INVISIBLE);
		
		linear = (LinearLayout) findViewById(R.id.job_class_list_linear);
		
		initializeAdapter();
	}

	/**
	 * 初始化ListView的适配器
	 */
	private void initializeAdapter() {
		id = getIntent().getStringExtra("id");
		
		adapter = new PaginationAdapter(artical);
		viewWhitProgress = new LoadMoreListViewWithProgress(this);
		listView = viewWhitProgress.getListView();

		listView.setAdapter(adapter);
		listView.setOnLoadMoreListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				loadDataTask();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (((RecApp) getApplicationContext()).isNetWorkConnection) {

					Intent intent = new Intent(BusinessSchoolListActivity.this,
							SchoolListInfoActivity.class);
					intent.putExtra("id", artical.get(position).getId());
					startActivity(intent);

				} else {
					Toast toast2 = Toast.makeText(BusinessSchoolListActivity.this,
							"网络连接断开，请检查网络", Toast.LENGTH_SHORT);
					toast2.setGravity(Gravity.CENTER, 0, 0);
					toast2.show();
				}

			}
		});

		linear.addView(viewWhitProgress);
		
		loadDataTask();
	}
	
	private void loadDataTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "businessCollegePostList");
		map.put("collegeId", id);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {

						String data = response.getString("dataList");
						JSONArray list = new JSONArray(data);

						for (int i = 0; i < list.length(); i++) {
							Artical info = new Artical();
							JSONObject log = list.getJSONObject(i);
							info.setId(log.getString("id"));
							info.setTitle(log.getString("name"));

							artical.add(info);
						}

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

				// We need notify the adapter that the data have been changed
				adapter.notifyDataSetChanged();
				viewWhitProgress.show();

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

	}
	
	class PaginationAdapter extends BaseAdapter {
		List<Artical> newsItems;

		public PaginationAdapter(List<Artical> newsitems) {
			this.newsItems = newsitems;
		}

		@Override
		public int getCount() {
			return newsItems.size();
		}

		@Override
		public Object getItem(int position) {
			return newsItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.job_class_items,
						null);
				// view.setLayoutParams(new );
			}
			// 标题
			TextView tvTitle = (TextView) view.findViewById(R.id.newstitle);
			tvTitle.setText(newsItems.get(position).getTitle());
			return view;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("BusinessSchoolListActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
